// 实体
// 预约工单
import * as $iframe from '@/utils/report/iframe/iframe'
export const entityScript = {
  init: function(scriptJson) {
    // 判断是否存在dtms
    if (!Object.prototype.hasOwnProperty.call(scriptJson, 'doms')) {
      scriptJson.doms = {}
    }
    if (!Object.prototype.hasOwnProperty.call(scriptJson['doms'], 'appointmentOrd')) {
      scriptJson.doms.appointmentOrd = entityScript
    }
  },

  infoButtonClick: function(vueObject, data, btnEvent) {
    return false
  },
  // 按钮点击
  buttonClick: function(vueObject, row, btnEvent) {
    if (btnEvent === 'details') {
      this.detail(vueObject, row)
      return true
    } else if (btnEvent === 'closeIframe') {
      this.closeIframe(vueObject, row)
      return true
    } else {
      return false
    }
  },
  // 详情
  detail: function name(vueObject, row) {
    // const backUrl = vueObject.$route.path
    if (!row) {
      vueObject.$message({
        type: 'warning',
        message: '请选择一行数据!'
      })
    } else {
      const urlCode = '001003'
      const params = {
        // show: true,
        title: '预约工单',
        code: 'sequenceinfo'
      }
      vueObject.iframes.push(params)

      const childIframeUrl = window.location.origin + '/#' + '/report/sequenceinfo'
      vueObject.$nextTick(() => {
        const orderDom = document.querySelectorAll('#iframe')[[document.querySelectorAll('#iframe').length - 1]]
        const data = {
          code: 'sequenceinfo',
          iframeUrl: childIframeUrl,
          params: {
            urlCode: urlCode,
            code: row.code
          }
        }
        $iframe.creatIframe(orderDom, data, vueObject.iframeDom, vueObject)
      })
    }
  },
  closeIframe: function(vueObject, code) {
    $iframe.closeIframe(code, vueObject.iframes, vueObject.iframeDom)
  },
  // 是否是Iframe开窗
  isinfoPop(code) {
    const infoPopList = ['machineWorkCode']
    return infoPopList.includes(code)
  },
  // 开iframe
  openIframe(code, iframeparams, vueObject) {
    const params = {
      // show: true,
      title: `请选择`,
      code: 'textt',
      bornCode: code
    }
    vueObject.iframes.push(params)
    const childIframeUrl = window.location.origin + '/#' + '/textt'
    vueObject.$nextTick(() => {
      const orderDom = document.querySelectorAll('#iframe')[[document.querySelectorAll('#iframe').length - 1]]
      const data = {
        code: 'textt',
        iframeUrl: childIframeUrl,
        params: iframeparams
      }
      $iframe.creatIframe(orderDom, data, vueObject.iframeDom, vueObject)
    })
  },
  // info组件开窗信息 code组件code type 表头或表身  row 表身当前行 data 当前info页data
  infoPop(type, line, code, tableCode, currentRow, data, popupModel, popupEntity, popupExpression, valueExpression, listThis) {
    let params
    if (code === 'machineWorkCode') {
      if (type === 'body') {
        if (type === 'body' && Object.keys(currentRow).length === 0) { return } else if (type === 'body' && Object.keys(currentRow).length !== 0) {
          const row = JSON.parse(JSON.stringify(currentRow))
          row.line = line
          row.tableCode = tableCode
          params = {
            machineCode: data.head.machineCode
          }
          this.openIframe(code, params, listThis)
        }
      } else {
        params = {
          machineCode: data.head.machineCode
        }
        this.openIframe(code, params, listThis)
      }
    }
  },
  receivePopInfo(data, infoThis) {
    if (data.bornCode === 'machineWorkCode') {
      this.machineWorkCodeA(data.params, infoThis)
    }
  },
  machineWorkCodeA(data, infoThis) {
    if (infoThis.tableChangeData.type === 'body' && infoThis.tableChangeData.line !== null) {
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machineWorkId', data.mapId)
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machineWorkName', data.mapName)
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machineWorkCode', data.mapCode)
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machinePointId', data.pointId)
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machinePointCode', data.pointCode)
      infoThis.$set(infoThis.data.body.loopOrdItem[infoThis.tableChangeData.line], 'machinePointName', data.pointName)
    } else {
      infoThis.$set(infoThis.data.head, 'machineWorkId', data.mapId)
      infoThis.$set(infoThis.data.head, 'machineWorkName', data.mapName)
      infoThis.$set(infoThis.data.head, 'machineWorkCode', data.mapCode)
      infoThis.$set(infoThis.data.head, 'machinePointId', data.pointId)
      infoThis.$set(infoThis.data.head, 'machinePointCode', data.pointCode)
      infoThis.$set(infoThis.data.head, 'machinePointName', data.pointName)
    }
  },
  // 表头样式初始化
  onInfoHeadFrameInit: function(vueObject, headFrameData) {
    //
  },
  // 表身样式初始化
  onInfoBodyFrameInit: function(vueObject, bodyFrameData) {
    //
  },
  // 表身新建行
  onAddBodyRow: function(vueObject, bodyName, itemData) {
    //
    //
  },
  // 表头改变事件
  onHeadChange: function(vueObject, data, fieldName, oldValue, newValue) {
    //
    //
    //
  },
  // 表身改变事件
  onBodyChange: function(vueObject, data, bodyName, lineNum, fieldName, oldValue, newValue) {
    //
    //
    //
    //
    //
  }
}
